package com.zkingsoft.services.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.TransferRecordDao;
import com.zkingsoft.dao.UserdataDao;
import com.zkingsoft.model.Dictionary;
import com.zkingsoft.model.Member;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.TransferRecord;
import com.zkingsoft.model.Userdata;
import com.zkingsoft.newdao.MemberDao;
import com.zkingsoft.newdao.PdkUserDao;
import com.zkingsoft.plugin.util.DBHelper;
import com.zkingsoft.services.UserdataService;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-07-04 15:51
 */
@Service("userdataService")
public class  UserdataServiceImpl implements    UserdataService  {
	/**
	 * 正式数据库连接
	 */
	private static final String DB_NAME = "jjzxpdk_userdata";
	private static final String IP = "112.74.86.174";
	private static final String USER_NAME = "webuser";
	private static final String PWD = "1q2w3e4r5t";
	private Dictionary dict1 = new Dictionary("game","root","root","127.0.0.1");
//	private Dictionary dict1 = new Dictionary(DB_NAME,USER_NAME,PWD,IP);
	private MemberDao memberDao = new MemberDao();
	
	private PdkUserDao pdkUserDao = new PdkUserDao();
	@Autowired
	private UserdataDao userdataDao;
	@Autowired
	private TransferRecordDao transferDao;
	
	
	@Override
	public int add(Userdata userdata){
		return userdataDao.insert(userdata);
	}
   	
    @Override
	public int modify(Userdata userdata){
		return userdataDao.update(userdata);
	}
	
	

	@Override
	public int removeById(Integer userid){
		return userdataDao.deleteById(userid);
	}
	
	@Override
	public List<Userdata> findInPage(Userdata userdata,  PaginationVO pageVo){
		return userdataDao.selectInPage(userdata , pageVo);
	}
	
	@Override
	public List<Userdata> findByModel(Userdata userdata){
		return userdataDao.selectByModel(userdata);
	}
	
	@Override
	public int  findTotal(Userdata userdata){
	
		return userdataDao.selectTotalRecord(userdata);
	
	}
	
	@Override
	public Userdata  findById(Integer userid){
	
		return userdataDao.selectById(userid);
	
	}

	@Override
	public int remove(List<Long> list) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int removeById(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Userdata findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Userdata findByWeixinId(Integer userId, String weixinId) {
		// TODO Auto-generated method stub
		return userdataDao.selectByWeixinId(userId,weixinId);
	}

	@Override
	public Userdata findByWeixinId(String weixinId) {
		// TODO Auto-generated method stub
		return userdataDao.selectByWeiXin(weixinId);
	}

	@Override
	public int updateDiamond(Integer gameId, int diamondNum) {
		// TODO Auto-generated method stub
		return userdataDao.updateDiamond(gameId,diamondNum);
	}

   	
	@Override
	public Member  findByMemberId(Integer id,Dictionary dict){
		return memberDao.selectById(id,dict);
	}


	@Override
	public Member findByTel(String tel,Dictionary dict) {
		// TODO Auto-generated method stub
		return memberDao.selectByTel(tel,dict);
	}
	
	@Override
	@Transactional(value="transactionManager",rollbackFor=java.lang.Exception.class)
	public int clubInPdk(Member member, Userdata userData,int outMoney,Dictionary dict) {
		int otherMoney = 0;
		int num = 0;
		int num1 = 0;
		Connection conn = DBHelper.getConnection(dict);
		Connection conn1 = DBHelper.getConnection(dict1);
		try {
			conn.setAutoCommit(false);
			conn1.setAutoCommit(false);
			num = pdkUserDao.updateDiamondById(userData.getUserid(), userData.getDiamondnum()+outMoney*10, conn1);
			//优先转充值积分
			if(outMoney<=member.getScore()){
				num1 = memberDao.updateScoreById(member.getId(),member.getScore()-outMoney,conn);
			}else{
				otherMoney = outMoney-member.getScore();
				num1 = memberDao.updateOtherScoreById(member.getId(),member.getRewardscore()-otherMoney,conn);
			}
			//提交事务
			if(num>0&&num1>0){
				conn1.commit();
				try {
					conn.commit();
					return 1;
				} catch (Exception e) {
					e.printStackTrace();
					//添加转账失败记录
					addTransferRecord(member, userData, outMoney, dict, conn1);
				}
			}
		} catch (Exception e) {
			//回滚事务
			try {
				conn.rollback();
				conn1.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			DBHelper.closeConnection(conn);
			DBHelper.closeConnection(conn1);
		}
		return 0;
	}

	@Override
	@Transactional(value="transactionManager",rollbackFor=java.lang.Exception.class)
	public int pdkOutClub(Member member, Userdata userData,int outMoney,Dictionary dict) {
		int num = 0;
		int num1 = 0;
		Connection conn = DBHelper.getConnection(dict);
		Connection conn1 = DBHelper.getConnection(dict1);
		try {
			conn.setAutoCommit(false);
			conn1.setAutoCommit(false);
			num = pdkUserDao.updateDiamondById(userData.getUserid(),userData.getDiamondnum()-outMoney*10,conn1);
//			String a = null;
//			a.toString();
			num1 = memberDao.updateScoreById(member.getId(),member.getScore()+outMoney,conn);
			//提交事务
			if(num>0&&num1>0){
				conn1.commit();
				try {
					conn.commit();
					return 1;
				} catch (Exception e) {
					e.printStackTrace();
					addTransferRecord(member, userData, outMoney, dict, conn1);
				}
			}
		} catch (Exception e) {
			//回滚事务
			try {
				conn.rollback();
				conn1.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			DBHelper.closeConnection(conn);
			DBHelper.closeConnection(conn1);
		}
		return 0;
	}

	private void addTransferRecord(Member member, Userdata userData,
			int outMoney, Dictionary dict, Connection conn1) throws Exception {
		//添加转账失败记录
		TransferRecord transfer = new TransferRecord();
		transfer.setMemberId(member.getId());
		transfer.setDictId(dict.getId());
		transfer.setCreateTime(new Date().getTime());
		transfer.setMoney(outMoney);
		transfer.setStatus(2);
		transferDao.insert(transfer);
		//将转出的修改回去
		int res = pdkUserDao.updateDiamondById(userData.getUserid(),userData.getDiamondnum()+outMoney,conn1);
		if(res>0){
			//修改转账记录表的状态
			transferDao.updateStatusById(transfer.getId(), new Date().getTime());
		}
	}
	public void updateToken(String token,Integer weixinId) {
		userdataDao.updateToken(token,weixinId);
	}
	
	
}